02 / 05

What is the N+1 query problem and how do you solve it in Go?

The N+1 problem occurs when fetching N parent records then issuing N individual queries for related data. Solve it with JOIN queries, batch loading with IN clauses, or ORM preloading.

N+1 problem and solutions
Detection and prevention
  1. 1

    Enable query logging in development: pgx log level debug or EXPLAIN ANALYZE suspicious queries

  2. 2

    Use sqlc or GORM with Preload to generate/use optimized batch queries

  3. 3

    DataLoader pattern for GraphQL: batch all IDs within a request cycle then fetch in one query

  4. 4

    Look for database query counts in your APM (Datadog, New Relic) — sudden spikes indicate N+1

  5. 5

    Code review: any DB call inside a loop is a red flag — prefer batch operations

Difficulty: 5/10
Topics: ORM usage, query optimization, performance profiling

Scenario Questions

0-2 years experience
  1. 1

    You have a Go function that fetches a list of users and then, inside a loop, queries the DB for each user's profile. What problem might you see and how would you rewrite it?

  2. 2

    If the number of database queries grows proportionally to the number of items returned, what steps would you take in Go to reduce that count?

2-5 years experience
  1. 1

    We added an endpoint that returns orders with their line items, and latency doubled after release. Walk me through how you'd check if an N+1 query is the cause and how you'd fix it in Go.

  2. 2

    Our service uses GORM and we see a spike in DB connections when loading a product catalog. Explain why an N+1 pattern might be happening and which GORM features you could use to prevent it.

5-8 years experience
  1. 1

    Design a Go data‑access layer that avoids N+1 queries across multiple services, considering caching, batching, and transaction boundaries. What trade‑offs do you evaluate?

  2. 2

    A microservice aggregates data from three tables using separate queries per row, causing N+1 at millions of rows. How would you refactor the queries and what impact on consistency and latency would you expect?

  3. 3

    How would you instrument a Go application in production to detect N+1 query patterns and what automated mitigations could you deploy?

8+ years experience
  1. 1

    We are migrating a legacy monolith written in Go that suffers from pervasive N+1 queries to an event‑driven architecture. How would you plan the migration to eliminate N+1 while preserving business logic?

  2. 2

    Different teams use various ORMs, leading to inconsistent eager loading handling. As a staff engineer, how would you establish a company‑wide strategy to prevent N+1 queries and ensure maintainability?

  3. 3

    For a high‑throughput read‑heavy service serving billions of requests daily, discuss architectural choices (read replicas, CQRS, materialized views) you would make to guarantee N+1 queries never become a bottleneck.

Follow-up Questions

  • What are the downsides of using a large join to eliminate N+1?
  • How would you measure the performance improvement after refactoring?
  • Can you describe a scenario where eager loading might still cause issues?